package com.example.campusrear.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.campusrear.Service.ClassService;
import com.example.campusrear.Service.CourseService;
import com.example.campusrear.Service.LeaverecordService;
import com.example.campusrear.Service.StudentService;
import com.example.campusrear.Utils.R;
import com.example.campusrear.entity.*;
import com.example.campusrear.entity.Class;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/leave")
public class LeaverecordController {
    @Resource
    private LeaverecordService leaverecordService;
    @Resource
    private ClassService classService;
    @Resource
    private StudentService studentService;

    @Resource
    private CourseService courseService;

    @PostMapping("/save")
    public R<String> save(@RequestBody Leaverecord leaverecord){
        leaverecordService.save(leaverecord);
        return R.success("已发起申请");
    }

    @PostMapping("/update")
    public R<String> update(@RequestBody Leaverecord leaverecord){
        leaverecordService.updateById(leaverecord);
        return R.success("修改成功");
    }

    @DeleteMapping("/delete/{lId}")
    public R<String> del(@PathVariable Long lId){
        leaverecordService.removeById(lId);
        return R.success("删除成功");
    }
    @GetMapping("/page")
    public R<Page<LeaverecordDto>> page(int page, int pageSize, Long sId){
        Page<Leaverecord> leaverecordPage = new Page<>(page ,pageSize);
        Page<LeaverecordDto> leaverecordDtoPage = new Page<>();
        LambdaQueryWrapper<Leaverecord> wrapper = new LambdaQueryWrapper<>();
        if (sId!=null){
            wrapper.eq(Leaverecord::getStudentId,sId);
        }
        leaverecordService.page(leaverecordPage,wrapper);
        BeanUtils.copyProperties(leaverecordPage,leaverecordDtoPage,"records");
        List<Leaverecord> records = leaverecordPage.getRecords();
        List<LeaverecordDto> leaverecordDtos = records.stream().map(item->{
            LeaverecordDto leaverecordDto = new LeaverecordDto();
            BeanUtils.copyProperties(item,leaverecordDto);
            Long studentId = leaverecordDto.getStudentId();
            Long classId = leaverecordDto.getClassId();
            Long courseId = leaverecordDto.getCourseId();
            Student student = studentService.getById(studentId);
            Class aClass = classService.getById(classId);
            Course course = courseService.getById(courseId);
            leaverecordDto.setSName(student.getStudentName());
            leaverecordDto.setCName(aClass.getClassName());
            leaverecordDto.setCCName(course.getCourseName());
            return leaverecordDto;
        }).collect(Collectors.toList());
        leaverecordDtoPage.setRecords(leaverecordDtos);
        return R.success(leaverecordDtoPage);
    }
}
